//
//  LXTabBarView.m
//  微博
//
//  Created by imera on 15-9-11.
//  Copyright (c) 2015年 imera. All rights reserved.
//

#import "LXTabBarView.h"
#import "LXTabBarButton.h"

@interface LXTabBarView()

@property (nonatomic, weak) UIButton *currentBtn;

@property (nonatomic, strong) NSMutableArray *Btns;

@property (nonatomic, weak) UIButton *addBtn;

@end

@implementation LXTabBarView

- (NSMutableArray *)Btns
{
    if(_Btns == nil)
    {
        _Btns = [NSMutableArray array];
    }
    return _Btns;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if(!IOS7){
            [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageWithName:@"tabbar_background"]]];
        }
        //添加加号
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [btn setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [btn setImage:[UIImage imageWithName:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageWithName:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(compose) forControlEvents:UIControlEventTouchDown];
        
        [self addSubview:btn];
        
        self.addBtn = btn;
 
    }
    return self;
}

- (void)compose
{
    if([self.MyDelegate respondsToSelector:@selector(tabBarDidClicked:)]){
        [self.MyDelegate tabBarDidClicked:self];
    }
}

- (void)addTabBarButton:(UITabBarItem *)item
{
    LXTabBarButton *btn = [[LXTabBarButton alloc]init];
    //设置按钮内容
    btn.item = item;

    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    
    [self addSubview:btn];
    [self.Btns addObject:btn];
    
    NSMutableArray *arr = [NSMutableArray array];
    
    [arr addObject:btn];
    
}

- (void)btnClick:(UIButton *)btn
{
    self.currentBtn.selected = NO;
    
    if([self.MyDelegate respondsToSelector:@selector(tabBar)]){
       [self.MyDelegate tabBar:self selectedButtonFrom:(int)self.currentBtn.tag to:(int)btn.tag];
    }
    
    btn.selected = YES;
    self.currentBtn = btn;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //设置加号的位置和大小
    CGFloat addBtnX = self.frame.size.width * 0.5;
    CGFloat addBtnY = self.frame.size.height * 0.5;
    CGFloat addBtnW = self.addBtn.currentBackgroundImage.size.width;
    CGFloat addBtnH = self.addBtn.currentBackgroundImage.size.height;
    
    self.addBtn.bounds = CGRectMake(0, 0, addBtnW, addBtnH);
    self.addBtn.center = CGPointMake(addBtnX, addBtnY);

    //设置btn的frame
    for (int index=0;index<self.Btns.count;index++) {
        LXTabBarButton *btn = self.Btns[index];

        CGFloat btnW = self.frame.size.width / self.subviews.count;
        CGFloat btnX = index * btnW;
        if(index > 1){
            btnX += btnW;
        }
        CGFloat btnY = 0;
        CGFloat btnH = self.frame.size.height;
        
        if(index == 0){
            [self btnClick:btn];
        }
        btn.tag = index;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }

}

@end
